import os
import random

import numpy as np

from Ling.common.utils.OcrUtils import get_txt_from_file, \
    get_dict_mapping_value, draw_to_image, msyh_font, simsun_font, simfang_font, check_font_files, get_dict_from_file, \
    merge_dict, clear_not_in_char_of_file, get_dict_dot, get_dict_from_files, gen_all_font_of_folder

root = 'D:/workspace/python/train_data/'
dictRoot = root + 'train_data_configs/words/'
reader_root = 'D:/ocr/ocr_resources/reader/'
font_root = 'D:/ocr/ocr_resources/reader/fonts/'


def gen_front_image_2_check():
    # 生成 jpg 检查字体对in_chars,in_chars_dot,char_mapping value的支持
    chars_text1 = get_txt_from_file(dictRoot + 'font/in_chars.txt').replace('\n', '')
    chars_text2 = get_txt_from_file(dictRoot + 'font/in_chars_dot.txt').replace('\n', '')
    # chars_text3 = get_txt_from_file(dictRoot + 'char_mapping.txt').replace('\n', '')
    mapping_values = get_dict_mapping_value()
    draw_to_image(msyh_font, chars_text1, dictRoot + 'font/in_chars_msyh.jpg')
    draw_to_image(msyh_font, chars_text2, dictRoot + 'font/in_chars_dot_msyh.jpg')
    draw_to_image(msyh_font, ''.join(mapping_values), dictRoot + 'font/char_mapping_msyh.jpg')

    draw_to_image(simfang_font, chars_text1, dictRoot + 'font/in_chars_simfang.jpg')
    draw_to_image(simfang_font, chars_text2, dictRoot + 'font/in_chars_dot_simfang.jpg')
    draw_to_image(simfang_font, ''.join(mapping_values), dictRoot + 'font/char_mapping_simfang.jpg')

    draw_to_image(simsun_font, chars_text1, dictRoot + 'font/in_chars_simsun.jpg')
    draw_to_image(simsun_font, chars_text2, dictRoot + 'font/in_chars_dot_simsun.jpg')
    draw_to_image(simsun_font, ''.join(mapping_values), dictRoot + 'font/char_mapping_simsun.jpg')


def check_in_char():
    # 检查字体对in_chars的自持
    in_chars, out_chars = check_font_files(msyh_font, [dictRoot + 'font/in_chars.txt'])
    print(out_chars)
    in_chars, out_chars = check_font_files(simfang_font, [dictRoot + 'font/in_chars.txt'])
    print(out_chars)
    in_chars, out_chars = check_font_files(simsun_font, [dictRoot + 'font/in_chars.txt'])
    print(out_chars)


def process_clear_not_in_char_of_file():
    mapping_values = get_dict_mapping_value()
    dict_in = merge_dict(get_dict_from_file(dictRoot + 'font/in_chars.txt') + get_dict_dot(),
                         mapping_values)
    clear_not_in_char_of_file(reader_root + 'all_words/in_chars_words.txt',
                              dict_in,
                              reader_root + 'all_words/in_chars_words_clear.txt')


def char_num_from_files(files):
    result = set('')
    for file in files:
        dict = get_dict_from_file()
        result = merge_dict(result, dict)
    return result


def get_all_font_chars(font_root, out_path):
    fonts = [f for f in os.listdir(font_root) if f.endswith('.txt')]
    files = []
    for font in fonts:
        files.append(font_root + font)
    all_dicts = get_dict_from_files(files)
    with open(out_path, 'w', encoding='utf-8') as f:
        for char in all_dicts:
            f.write(char + '\n')


if __name__ == "__main__":
    print(1)
    # ****************************从content到dict全过程 begin********************#
    # # 通过多线程 转换_content==>_words
    # process_dict_words_of_folder(['D:/ocr/ocr_resources/reader/contents/'], 200, 10)  # 这里为每本书期望字符出现的次数

    # 从all_words文件中提取words,追加到单个字对应的txt中
    # folder_2_single_dict_file(reader_root + 'words_10_no_dot', reader_root + 'dict_10_no_dot')
    #
    # get_char_and_line_num('D:/ocr/ocr_resources/reader/dict_10_no_dot/',
    #                       'D:/ocr/ocr_resources/reader/all_words/char_num_sort_10_no_dot.txt')
    #
    # 根据in_chars.txt从字典库中抽取随机的行数组成in_chars_words.txt,每行需要清理掉不在字体库支持的文字
    # get_dict_words_from_folder(get_dict_from_file(dictRoot + 'font/in_chars.txt'), reader_root + 'dict_10_no_dot/', 200,
    #                            reader_root + 'all_words/in_chars_words_10_no_dot.txt')
    # 后续请在text_render中执行
    # ****************************从content到dict全过程 end********************#

    # print('\r\n' == '')
    chars_text = get_txt_from_file(dictRoot + 'font/in_chars.txt')
    # check_all_font_of_folder('D:/ocr/ocr_resources/reader/fonts/', chars_text, 'D:/ocr/ocr_resources/reader/fonts/')
    # gen_all_font_of_folder('D:/ocr/ocr_resources/字体/', 'D:/ocr/ocr_resources/reader/fonts2/')
    # gen_all_font_of_folder('D:/ocr/ocr_resources/字体/01方正字体 （508款）/', 'D:/ocr/ocr_resources/reader/fonts2/')
    # gen_all_font_of_folder('D:/ocr/ocr_resources/字体/10微软字体 （36款/', 'D:/ocr/ocr_resources/reader/fonts2/')
    gen_all_font_of_folder('D:/ocr/ocr_resources/字体/挑选/', chars_text, 'D:/ocr/ocr_resources/reader/fonts/', True)

    # D:/ocr/ocr_resources/字体/01方正字体 （508款）/方正楷体拼音字库.TTC

    # chars = get_font_chars('D:/ocr/ocr_resources/reader/fonts/Deng.ttf')
    # print(chars)

    # get_all_font_chars(font_root, 'D:/workspace/python/train_data/train_data_configs/words/result_all_font.txt')

    # 1.生成有范围的可以重复的随机数，生成519个可以重复的随机数，随机数的范围是0-100,0可以取到，100取不到。
    # ran = np.random.randint(0, '生成519个可以重复的随机数', 200)
    # ran = random.sample('生成519个可以重复的随机数', 100)
    # ran = random.choice('生成519个可以重复的随机数')
    # print(ran)
